package com.sedric.utils;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import org.apache.commons.lang.StringUtils;
import org.apache.tools.zip.ZipEntry;
import org.apache.tools.zip.ZipOutputStream;

public class ZipUtils {

    private static final int BUFFERSIZE = 8192;

    public static void zip(String zipFileName, String... sourcefile) {

        if (null == sourcefile || sourcefile.length == 0) {
            return;
        }
        File[] files = new File[sourcefile.length];

        for (int i = 0; i < sourcefile.length; i++) {
            if (StringUtils.isBlank(sourcefile[i])) {
                continue;
            }
            files[i] = new File(sourcefile[i]);
        }
        zip(zipFileName, files);
    }

    public static void zip(String zipFileName, File[] files) {

        if (StringUtils.isBlank(zipFileName)) {
            return;
        }
        if (null == files || files.length == 0) {
            return;
        }

        File zipFile = new File(zipFileName);

        if (zipFile.exists()) {
            FileUtils.delete(zipFile);
        }

        ZipOutputStream zos = null;
        FileOutputStream ops = null;

        try {
            ops = new FileOutputStream(zipFile);
            zos = new ZipOutputStream(ops);
            zos.setEncoding("GBK");

            String baseDir = "";

            for (File file : files) {

                if (null == file || !file.exists()) {
                    continue;
                }
                else if (file.isDirectory()) {
                    zipDir(zos, file, baseDir);
                }
                else {
                    zipFile(zos, file, baseDir);
                }
            }
            zos.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void zipDir(ZipOutputStream zos, File dir, String baseDir)
            throws FileNotFoundException {

        File[] files = dir.listFiles();

        baseDir = baseDir + dir.getName() + "/";

        for (File file : files) {
            if (file.isDirectory()) {
                zipDir(zos, file, baseDir);
            }
            else {
                zipFile(zos, file, baseDir);
            }
        }

    }

    public static void zipFile(ZipOutputStream zos, File file, String baseDir) {
        try {
            BufferedInputStream bis = new BufferedInputStream(
                    new FileInputStream(file));

            ZipEntry entry = new ZipEntry(baseDir + file.getName());

            zos.putNextEntry(entry);

            byte[] data = new byte[BUFFERSIZE];

            int count = 0;

            while ((count = bis.read(data, 0, BUFFERSIZE)) != -1) {

                zos.write(data, 0, count);

            }
            bis.close();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
        String[] file = { "E:/photo" };

        String zipFile = "E:/photo.zip";
        zip(zipFile, file);
    }

}
